﻿Imports System
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities
    Friend NotInheritable Class StateDropDownEditor
        Inherits UITypeEditor
        ' Methods
        Private Sub dataSourceDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me._editorService.CloseDropDown
            Me._selectedObject = Nothing
            Dim box As ListBox = [TryCast](sender,ListBox)
            If (box Is Nothing) Then
                Throw New ArgumentNullException("sender")
            End If
            If (box.SelectedIndex >= 0) Then
                Me._selectedObject = box.Items.Item(box.SelectedIndex)
            End If
        End Sub

        Public Overrides Function EditValue(ByVal typeDescriptorContext As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal value As Object) As Object
            If (typeDescriptorContext Is Nothing) Then
                Throw New ArgumentNullException("typeDescriptorContext")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Me._editorService = DirectCast(serviceProvider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            Me._context = typeDescriptorContext
            Dim dropDownList As New ListBox { _
                .BorderStyle = BorderStyle.None _
            }
            Dim instance As Activity = [TryCast](Me._context.Instance,Activity)
            If (instance Is Nothing) Then
                Dim objArray As Object() = [TryCast](Me._context.Instance,Object())
                If ((Not objArray Is Nothing) AndAlso (objArray.Length > 0)) Then
                    instance = DirectCast(objArray(0), Activity)
                End If
            End If
            Me.PopulateDropDownList(dropDownList, instance)
            AddHandler dropDownList.SelectedIndexChanged, New EventHandler(AddressOf Me.dataSourceDropDown_SelectedIndexChanged)
            Me._editorService.DropDownControl(dropDownList)
            If ((dropDownList.SelectedIndex <> -1) AndAlso (Not Me._selectedObject Is Nothing)) Then
                Return Me._selectedObject
            End If
            Return value
        End Function

        Private Sub FindStates(ByVal dropDownList As ListBox, ByVal parent As StateActivity)
            Dim activity As Activity
            For Each activity In parent.EnabledActivities
                Dim state As StateActivity = [TryCast](activity,StateActivity)
                If (Not state Is Nothing) Then
                    If StateMachineHelpers.IsLeafState(state) Then
                        dropDownList.Items.Add(state.QualifiedName)
                    Else
                        Me.FindStates(dropDownList, state)
                    End If
                End If
            Next
        End Sub

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.DropDown
        End Function

        Private Sub PopulateDropDownList(ByVal dropDownList As ListBox, ByVal activity As Activity)
            Dim state As StateActivity = StateMachineHelpers.FindEnclosingState(activity)
            If (Not state Is Nothing) Then
                Dim rootState As StateActivity = StateMachineHelpers.GetRootState(state)
                Me.FindStates(dropDownList, rootState)
            End If
        End Sub


        ' Fields
        Private _context As ITypeDescriptorContext
        Private _editorService As IWindowsFormsEditorService
        Private _selectedObject As Object
    End Class
End Namespace

